package com.gw.quotes.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.gw.quotes.R;
import com.gw.quotes.adapter.NoticeAdapter;
import com.gw.quotes.entity.business.GetInformationListEntity;
import com.gw.quotes.entity.business.GetInformationListEntity.Information;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GetInformationListServiceImpl;
import com.yxw.view.YXWXListView;
import com.yxw.view.YXWXListView.IXListViewListener;

public class NoticeActivity extends BaseActivity implements IXListViewListener {

	@ViewInject(id = R.id.notice_listview)
	private YXWXListView noticeListview;
	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;

	private NoticeAdapter noticeAdapter;
	private ArrayList<Information> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notice);
		initData();
		initView();
		noticeListview.startRefresh();
	}

	private void initData() {
		list = (ArrayList<Information>) aCache.getAsObject("Informations");
		noticeAdapter = new NoticeAdapter(this, R.layout.item_notice, list);
		noticeListview.setAdapter(noticeAdapter);

	}

	private void initView() {

		noticeListview.setPullLoadEnable(false);
		noticeListview.setXListViewListener(this);
		noticeListview.setAdapter(noticeAdapter);
	}

	private void requestNotices() {

		// 设置参数
		BaseServiceInter httpService = new GetInformationListServiceImpl();
		Request request = new Request();
		request.setPageSize(10);

		Response response = httpService.handler(request,
				new HttpCallBack<GetInformationListEntity>() {
					@Override
					public void onFinished(GetInformationListEntity response) {
						if (response != null
								&& response.getResult() == Response.RESULT_SUCCESS) {

							// 刷新或者第一次加载时的操作
							list = response.getInformationlist();
							if (list != null) {
								aCache.put("Informations",
										response.getInformationlist());
								initData();
							}
						}
						onLoad();
					}
				});

	}

	public void back(View v) {
		finish();
	}

	/**
	 * 结束刷新和加载
	 */
	private void onLoad() {
		noticeListview.stopRefresh();
		noticeListview.stopLoadMore();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		noticeListview.setRefreshTime(df.format(new Date()));
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRefresh() {
		requestNotices();
	}

}
